home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / BASERESPONSE.PY < prev    next >
Encoding:
Python Source  |  2000-06-08  |  8.1 KB  |  230 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''CGI Response Output formatter
  65.  
  66. $Id: BaseResponse.py,v 1.7 2000/06/08 15:47:35 jim Exp $'''
  67. __version__='$Revision: 1.7 $'[11:-2]
  68.  
  69. import string, types, sys, regex
  70. from string import find, rfind, lower, upper, strip, split, join, translate
  71. from types import StringType, InstanceType
  72.  
  73. class BaseResponse:
  74.     """Base Response Class
  75.  
  76.     What should be here?
  77.     """
  78.     debug_mode=None
  79.     _auth=None
  80.     _error_format='text/plain'
  81.     
  82.     # Allow (reluctantly) access to unprotected attributes
  83.     __allow_access_to_unprotected_subobjects__=1
  84.         
  85.     def __init__(self, stdout, stderr,
  86.                  body='', headers=None, status=None, cookies=None):
  87.         self.stdout=stdout
  88.         self.stderr=stderr
  89.         self.body=body
  90.         if headers is None: headers={}
  91.         self.headers=headers
  92.         self.status=status
  93.         if cookies is None: cookies={}
  94.         self.cookies=cookies
  95.     
  96.     def setStatus(self, status, reason=None):
  97.         self.status=status
  98.  
  99.     def setHeader(self, name, value):
  100.         self.headers[name]=value
  101.  
  102.     __setitem__=setHeader
  103.  
  104.     def outputBody(self):
  105.         """Output the response body"""
  106.         self.stdout.write(str(self))
  107.  
  108.     def setBody(self, body):
  109.         self.body=body
  110.  
  111.     def getStatus(self):
  112.         'Returns the current HTTP status code as an integer. '
  113.         return self.status
  114.  
  115.     def setCookie(self,name,value,**kw):
  116.         '''\
  117.         Set an HTTP cookie on the browser
  118.  
  119.         The response will include an HTTP header that sets a cookie on
  120.         cookie-enabled browsers with a key "name" and value
  121.         "value". This overwrites any previously set value for the
  122.         cookie in the Response object.
  123.         '''
  124.         cookies=self.cookies
  125.         if cookies.has_key(name):
  126.             cookie=cookies[name]
  127.         else: cookie=cookies[name]={}
  128.         for k, v in kw.items():
  129.             cookie[k]=v
  130.         cookie['value']=value
  131.  
  132.     def appendBody(self, body):
  133.         self.setBody(self.getBody() + body)
  134.  
  135.     def getHeader(self, name):
  136.          '''\
  137.          Get a header value
  138.          
  139.          Returns the value associated with a HTTP return header, or
  140.          "None" if no such header has been set in the response
  141.          yet. '''
  142.          return self.headers.get(name, None)
  143.  
  144.     def __getitem__(self, name):
  145.         'Get the value of an output header'
  146.         return self.headers[name]
  147.  
  148.     def getBody(self):
  149.         'Returns a string representing the currently set body. '
  150.         return self.body
  151.  
  152.     def __str__(self):
  153.         return str(self.body)
  154.  
  155.     def __repr__(self):
  156.         return '%s(%s)' % (self.__class__.__name__, `self.body`)
  157.  
  158.     def flush(self): pass
  159.  
  160.     def write(self,data):
  161.         """\
  162.         Return data as a stream
  163.  
  164.         HTML data may be returned using a stream-oriented interface.
  165.         This allows the browser to display partial results while
  166.         computation of a response to proceed.
  167.  
  168.         The published object should first set any output headers or
  169.         cookies on the response object.
  170.  
  171.         Note that published objects must not generate any errors
  172.         after beginning stream-oriented output. 
  173.  
  174.         """
  175.         self.body=self.body+data
  176.  
  177.     def exception(self, fatal=0, info=None):
  178.         """Handle an exception.
  179.  
  180.         The fatal argument indicates whether the error is fatal.
  181.  
  182.         The info argument, if given should be a tuple with an
  183.         error type, value, and traceback.
  184.         """
  185.  
  186.     def notFoundError(self, v=''):
  187.         """Generate an error indicating that an object was not found.
  188.         """
  189.         raise 'Not Found', v
  190.  
  191.     def debugError(self, v=''):
  192.         """Raise an error with debigging info and in debugging mode"""
  193.         raise 'Debug Error', v
  194.  
  195.     def badRequestError(self, v=''):
  196.         """Raise an error indicating something wrong with the request"""
  197.         raise 'Bad Request', v
  198.  
  199.     def forbiddenError(self, v=''):
  200.         """Raise an error indicating that the request cannot be done"""
  201.         raise 'Forbidden', v
  202.  
  203.     def unauthorized(self):
  204.         """Raise an eror indicating that the user was not authizated
  205.  
  206.         Make sure to generate an appropriate challenge, as appropriate.
  207.         """
  208.         raise 'Unauthorized'
  209.